package com.pe.pgn.clubpgn.webapp.controller.promocionconsumo;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.support.SessionStatus;

import com.pe.pgn.clubpgn.domain.ClpmPromocionConsumo;
import com.pe.pgn.clubpgn.service.PromocionConsumoService;
import com.pe.pgn.clubpgn.webapp.controller.BaseFormController;

@Controller
@RequestMapping("/promocionconsumo/**")
public class PromocionConsumoController extends BaseFormController {

	@Autowired
	private PromocionConsumoService promocionConsumoService;
	
	@RequestMapping(value = "/promocionconsumolist.html")
	public @ModelAttribute("promocionConsumo") ClpmPromocionConsumo buscarPromocionesConsumo(
			@ModelAttribute("promocionConsumo") ClpmPromocionConsumo promocionConsumo, 
			@RequestParam(value = "search", required = false) String search, Model model){
		
		if(StringUtils.isBlank(search)){
			
			List<ClpmPromocionConsumo> promociones = promocionConsumoService.buscarPromocionesConsumo
						(null, null, Boolean.TRUE);
			
			model.addAttribute("promociones", promociones);
			return new ClpmPromocionConsumo();
		}
		
		List<ClpmPromocionConsumo> promociones = promocionConsumoService.buscarPromocionesConsumo
									(promocionConsumo.getDePromocionConsumo(),promocionConsumo.getStEstadoPromocionConsumo(), Boolean.FALSE);
		model.addAttribute("promociones", promociones);
		return promocionConsumo;
	}
	
	@RequestMapping(value = "/promocionconsumoform.html", method = RequestMethod.GET)
	public @ModelAttribute("promocionConsumo") ClpmPromocionConsumo verPromocionConsumo(
			@RequestParam(value = "id", required = false) Long id, Model model){
		
		if(id != null){
			
			ClpmPromocionConsumo promocionConsumo = promocionConsumoService.obtenerPromocionConsumo(id);
			return promocionConsumo;			
		}
		
		ClpmPromocionConsumo promocionConsumo = new ClpmPromocionConsumo();
		promocionConsumo.setStPromocionConsumo(Boolean.TRUE);
		
		return promocionConsumo;		
	}
	
	@RequestMapping(value = "/promocionconsumoform.html", method = RequestMethod.POST)
	public String guardarPromocionConsumo(@ModelAttribute("promocionConsumo") ClpmPromocionConsumo promocionConsumo,
			BindingResult result, SessionStatus status, HttpServletRequest request, Model model) {

		try {
			
			boolean esDescripcionRepetida = promocionConsumoService.esDescripcionRepetida(promocionConsumo);
			if(esDescripcionRepetida){
				saveError(request, getText("promocionconsumoform.es.descripcion.repetida", request.getLocale()));
				model.addAttribute("promocionConsumo", promocionConsumo);
				return "/promocionconsumo/promocionconsumoform";
			}
			
			if (result.hasErrors()) {
				model.addAttribute("promocionConsumo", promocionConsumo);
				return "/promocionconsumo/promocionconsumoform";
			}
			
			promocionConsumoService.guardarPromocionConsumo(promocionConsumo);
			saveMessage(request, getText("promocionconsumoform.added", promocionConsumo.getDePromocionConsumo(), 
					request.getLocale()));
			return "redirect:promocionconsumolist.html";
			
		} catch (Exception e) {
			saveError(request, getText("errors.general", request.getLocale()));
			model.addAttribute("promocionConsumo", promocionConsumo);
			return "/promocionconsumo/promocionconsumoform";
		}
	}	
	
	@RequestMapping(value = "/eliminarpromocionconsumo.html")
	public String eliminarPromocionConsumo(@RequestParam(value = "id", required = false) Long id, 
			HttpServletRequest request) {

		try {

			promocionConsumoService.eliminarPromocionConsumo(id);
			return "redirect:promocionconsumolist.html";
			
		} catch (Exception e) {
			saveError(request, getText("errors.general", request.getLocale()));
			return "redirect:promocionconsumoform.html?id=" + id;
		}
	}
}
